package com.lh.controller;

import com.lh.pojo.UserAddress;
import com.lh.pojo.bo.UserAddressBO;
import com.lh.service.UserAddressService;
import com.lh.utils.IMOOCJSONResult;
import com.lh.utils.MobileEmailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: foodie-dev
 * @description:
 * @author: LH
 * @create: 2022-05-17 17:00
 **/
@Api(value = "地址相关接口", tags = {"地址相关api接口"})
@RestController
@RequestMapping("/address")
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    /**
     * 1.查询收货地址列表
     * 2.修改收货地址
     * 3.新增收货地址
     * 4.删除收货地址
     * 5.设置默认收货地址
     *
     */

    @ApiOperation(value = "查询收货地址列表", notes = "查询收货地址列表", httpMethod = "POST",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/list")
    public IMOOCJSONResult list(
            @ApiParam(name = "用户Id", value = "用户Id", required = true)
            @RequestParam String userId
    ){
        if (StringUtils.isEmpty(userId)){
            return IMOOCJSONResult.errorMsg("用户Id为空!");
        }

        List<UserAddress> list = userAddressService.list(userId);

        return IMOOCJSONResult.ok(list);
    }

    @ApiOperation(value = "修改收货地址", notes = "修改收货地址", httpMethod = "POST",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/update")
    public IMOOCJSONResult update(
            @RequestBody UserAddressBO userAddressBO
    ){

        if (StringUtils.isEmpty(userAddressBO.getAddressId())){
            IMOOCJSONResult.errorMsg("用户地址ID为空!");
        }

        IMOOCJSONResult IMOOCJSONResult = checkAddress(userAddressBO);
        if (IMOOCJSONResult.getStatus() != 200){
            return IMOOCJSONResult;
        }

        userAddressService.update(userAddressBO);

        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "新增收货地址", notes = "新增收货地址", httpMethod = "POST",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/add")
    public IMOOCJSONResult add(
            @RequestBody UserAddressBO userAddressBO
    ){

        IMOOCJSONResult IMOOCJSONResult = checkAddress(userAddressBO);
        if (IMOOCJSONResult.getStatus() != 200){
            return IMOOCJSONResult;
        }

        userAddressService.add(userAddressBO);

        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "删除收货地址", notes = "删除收货地址", httpMethod = "POST",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/delete")
    public IMOOCJSONResult delete(
            @ApiParam(name = "用户Id", value = "用户Id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "用户地址Id", value = "用户地址Id", required = true)
            @RequestParam String addressId
    ){
        if (StringUtils.isEmpty(userId)){
            return IMOOCJSONResult.errorMsg("用户Id为空!");
        }
        if (StringUtils.isEmpty(addressId)){
            return IMOOCJSONResult.errorMsg("用户地址Id为空!");
        }

        userAddressService.delete(userId,addressId);

        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "设置默认收货地址", notes = "设置默认收货地址", httpMethod = "POST",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/setDefault")
    public IMOOCJSONResult setDefault(
            @ApiParam(name = "用户Id", value = "用户Id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "用户地址Id", value = "用户地址Id", required = true)
            @RequestParam String addressId
    ){
        if (StringUtils.isEmpty(userId)){
            return IMOOCJSONResult.errorMsg("用户Id为空!");
        }
        if (StringUtils.isEmpty(addressId)){
            return IMOOCJSONResult.errorMsg("用户地址Id为空!");
        }

        userAddressService.setDefault(userId,addressId);

        return IMOOCJSONResult.ok();
    }

    private IMOOCJSONResult checkAddress(UserAddressBO userAddressBO) {
        String receiver = userAddressBO.getReceiver();
        if (StringUtils.isBlank(receiver)) {
            return IMOOCJSONResult.errorMsg("收货人不能为空");
        }
        if (receiver.length() > 12) {
            return IMOOCJSONResult.errorMsg("收货人姓名不能太长");
        }

        String mobile = userAddressBO.getMobile();
        if (StringUtils.isBlank(mobile)) {
            return IMOOCJSONResult.errorMsg("收货人手机号不能为空");
        }
        if (mobile.length() != 11) {
            return IMOOCJSONResult.errorMsg("收货人手机号长度不正确");
        }
        boolean isMobileOk = MobileEmailUtils.checkMobileIsOk(mobile);
        if (!isMobileOk) {
            return IMOOCJSONResult.errorMsg("收货人手机号格式不正确");
        }

        String province = userAddressBO.getProvince();
        String city = userAddressBO.getCity();
        String district = userAddressBO.getDistrict();
        String detail = userAddressBO.getDetail();
        if (StringUtils.isBlank(province) ||
                StringUtils.isBlank(city) ||
                StringUtils.isBlank(district) ||
                StringUtils.isBlank(detail)) {
            return IMOOCJSONResult.errorMsg("收货地址信息不能为空");
        }

        return IMOOCJSONResult.ok();
    }


}
